package com.quanliang.org.crawlerquanliang.utils;

import com.quanliang.org.crawlerquanliang.pojo.Data;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

/**
 * Jsoup解析Html
 *
 * @author FYM
 * @data 2022/8/29 6:38
 */
public class JsoupUtil {
    //BaseDivClass值
    public static final String BASECLASSVALUE = "_3gcd_TVhABEQqCcXHsrIpT";
    //标题Class值
    public static final String TITLECALSSVALUE = "_3_JaaUmGUCjKZIdiLhqtfr";
    //发布日期Class值
    public static final String RELASECDATELASSVALUE = "_3TzAhzBA-XQQruZs-bwWjE";
    //阅读数量Class值
    public static final String READCOUNTCLASSVALUE = "_2gvAnxa4Xc7IT14d5w8MI1";
    //图片URLClass值
    public static final String IMGURLCLASSVALUE = "_2ahG-zumH-g0nsl6xhsF0s";


    /**
     * 通过class值获取标签对字符串
     *
     * @return 多个指定class值的标签集合
     */
    public static List<String> getBaseHTMLListByClass(String sourceHtml, String classValue) {
        Elements elements = Jsoup.parse(sourceHtml).getElementsByClass(classValue);
        Iterator<Element> elementIterator = elements.iterator();
        List<String> list = new ArrayList<>();
        while (elementIterator.hasNext()) {
            list.add(elementIterator.next().toString());
        }
        return list;
    }

    /**
     * 通过ClassValue获取htmlString
     */
    public static String getBaseHTMLOneByClass(String sourceHtml, String classValue) {
        Elements elements = Jsoup.parse(sourceHtml).getElementsByClass(classValue);
        return elements.html();
    }

    /**
     * 提取所有 class为BASECLASS的div
     */
    public static List<String> getBaseDiv(String sourceHTML) {
        return getBaseHTMLListByClass(sourceHTML, BASECLASSVALUE);
    }

    /**
     * 提取Data成品
     */
    public static List<Data> getDataList(String sourceHTML) {
        ArrayList<Data> ans = new ArrayList<>();
        List<String> bigDataList = getBaseDiv(sourceHTML);
        for (String s : bigDataList) {
            //标题
            String title = getTitle(s);
            //发布日期
            String releaseDate = getReleaseDate(s);
            //阅读数
            String readCount = getReadCount(s);
            //图片地址
            String imgURL = getImgURL(s);

            Data data = new Data();
            data.setTitle(title);
            data.setReleaseDate(releaseDate);
            data.setReadCount(readCount);
            data.setImgURL(imgURL);
            ans.add(data);
        }
        return ans;
    }

    /**
     * 获取图片URL
     */
    private static String getImgURL(String sourceHTML) {
        //依赖jsoup
        Document document = Jsoup.parse(sourceHTML);
        Elements select = document.select("img[src]");
        String src = select.attr("src");
        return src.isEmpty() ? "" : src;
    }

    /**
     * 获取阅读数
     */
    private static String getReadCount(String sourceHTML) {
        //获取完整div,获取所有test,提取纯数字
        Elements elements = Jsoup.parse(sourceHTML).getElementsByClass(READCOUNTCLASSVALUE);
        List<String> strings = elements.eachText();
        for (String s : strings) {
            if (RegularUtil.isOnlyNum(s)) return s;
        }
        return "";
    }

    /**
     * 获取发布时间
     */
    private static String getReleaseDate(String sourceHTML) {
        //先jsoup再正则
        String releaseDivHTML = getBaseHTMLOneByClass(sourceHTML, RELASECDATELASSVALUE);
        return RegularUtil.getReleaseDate(releaseDivHTML);
    }

    /**
     * 通过BaseDiv标签获取标题内容
     */
    private static String getTitle(String sourceHTML) {
        Elements elements = Jsoup.parse(sourceHTML).getElementsByClass(TITLECALSSVALUE);
        return elements.text();
    }

}
